package com.jingbit.ai.servo;

import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.jingbit.ai.AIApp;

import java.util.Timer;
import java.util.TimerTask;

public class ServoPatrol {
    public static final int MSG_CODE = 3000;
    public static final int MSG_TRACK = 3001;
    private int angle;  // 舵机当前角度
    private int direction;  // 舵机当前转动方向
    private final int maxAngle;  // 舵机最大角度
    private final int minAngle;  // 舵机最小角度
    private final int step;  // 舵机转动步长
    private volatile Timer timer;  // 定时器
    private Handler.Callback callback;

    public ServoPatrol(Handler.Callback callback, int initialAngle, int maxAngle, int minAngle, int step) {
        this.callback = callback;
        this.angle = initialAngle;
        this.maxAngle = maxAngle;
        this.minAngle = minAngle;
        this.step = step;
        this.direction = 1;  // 初始方向为正向
    }

    public void startPatrol() {
        if (timer != null) {
            return;
        }
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Log.d(AIApp.TAG, Thread.currentThread().getName() + " startPatrol angle: " + angle);
                if (direction == 1) {
                    angle = angle > maxAngle ? maxAngle : angle + step;
                    if (angle > maxAngle) {  // 转动到最大角度，改变方向
                        direction = -1;
                    }
                } else {
                    angle = angle < minAngle ? minAngle : angle - step;
                    if (angle < minAngle) {  // 转动到最小角度，改变方向
                        direction = 1;
                    }
                }
                if (callback != null) {
                    Message msg = Message.obtain();
                    msg.what = MSG_CODE;
                    msg.arg1 = angle;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        callback.handleMessage(msg);
                    }
                }
            }
        }, 0, 2000);  // 定时器每隔1秒触发一次
    }


    public boolean isPatrol() {
        return timer != null;
    }

    public void stopPatrol() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }
//
//    private ObjectInfo lastFace;
//
//    public void calculateAngle(ObjectInfo currFace, int frameWidth, int frameHeight) {
//        Log.d(AIApp.TAG, "ServoPatrol calculateAngle: " + currFace.toString());
//        if (lastFace == null) {
//            lastFace = currFace;
//            return;
//        }
//        // 定义最大旋转角度
//        double maxHorizontalAngle = 270.0; // 左右旋转最大角度为270度
//        double maxVerticalAngle = 180.0;   // 上下旋转最大角度为180度
//
//        // 定义上一个坐标（previousX, previousY）和当前坐标（currentX, currentY）
//        double previousX = lastFace.getX_I(); // 上一个X坐标
//        double previousY = lastFace.getY_I(); // 上一个Y坐标
//        double currentX = currFace.getX_I();  // 当前X坐标
//        double currentY = currFace.getY_I();  // 当前Y坐标
//
//        // 计算水平角度的移动距离
//        double horizontalDistance = currentX - previousX;
//        double horizontalAngle = (horizontalDistance / frameWidth) * maxHorizontalAngle;
//
//        // 计算垂直角度的移动距离
//        double verticalDistance = currentY - previousY;
//        double verticalAngle = (verticalDistance / frameHeight) * maxVerticalAngle;
//
//        // 将角度限制在最大角度范围内
//        horizontalAngle = Math.max(-maxHorizontalAngle, Math.min(maxHorizontalAngle, horizontalAngle));
//        verticalAngle = Math.max(-maxVerticalAngle, Math.min(maxVerticalAngle, verticalAngle));
////        if (horizontalAngle<5){
////            return;
////        }
//
//        Log.d(AIApp.TAG, "ServoPatrol calculateAngle end: " + horizontalAngle + "---" + verticalAngle);
//        lastFace = currFace;
//    }


    public static void main(String[] args) {
        ServoPatrol servoPatrol = new ServoPatrol(null, 0, 180, 0, 10);
        servoPatrol.startPatrol();

        // 假设巡逻3分钟后停止
        try {
            Thread.sleep(3 * 60 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        servoPatrol.stopPatrol();
    }
}
